package com.bluefox.hospital.controller;

import com.bluefox.common.web.dto.result.PageResultDTO;
import com.bluefox.common.web.dto.result.ResultDTO;
import com.bluefox.hospital.convertor.LeaderConvertor;
import com.bluefox.hospital.dto.LeaderDTO;
import com.bluefox.hospital.model.Leader;
import com.bluefox.hospital.repository.LeaderRepository;
import com.bluefox.hospital.service.LeaderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Leader的管理接口
 *
 * @author
 */
 @Slf4j
@RestController
@RequestMapping("/w/leaders")
public class LeaderController {
    @Autowired
    private LeaderService leaderService;
	@Autowired
    private LeaderRepository leaderRepository;
    @Autowired
    private LeaderConvertor leaderConvertor;

    /**
     * 获取分页数据
     *
     * @param pageable 分页+排序参数
     * @return 分页数据
     */
    @RequestMapping(value = "/s", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public PageResultDTO<LeaderDTO> search(final Pageable pageable) {
        final Page<Leader> models = this.leaderRepository.findAll(pageable);
        return this.leaderConvertor.toResultDTO(models);
    }

    /**
     * 取得详细数据
     *
     * @param id 资源ID
     * @return 资源详细
     */

    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
    public ResultDTO<LeaderDTO> get(@PathVariable final Long id) {
        final Leader model = this.leaderService.get(id);
        return this.leaderConvertor.toResultDTO(model);
    }

    /**
     * 新建操作
     *
     * @param leaderDTO 新建资源的DTO
     * @return 新建资源
     */

    @RequestMapping(method = RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
    public ResultDTO<LeaderDTO> create(@RequestBody @Valid final LeaderDTO leaderDTO) {
        final Leader model = this.leaderConvertor.toModel(leaderDTO);
        this.leaderService.create(model);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was created.", Leader.class.getSimpleName(), model.getId());
        }
        return this.leaderConvertor.toResultDTO(model);
    }

    /**
     * 更新操作
     *
     * @param id 更新资源的ID
     * @param leaderDTO 更新资源的DTO
     * @return 更新后资源
     */

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT, produces=MediaType.APPLICATION_JSON_VALUE)
    public ResultDTO<LeaderDTO> update(@PathVariable final Long id, @RequestBody @Valid final LeaderDTO leaderDTO) {
        leaderDTO.setId(id);
        final Leader model = this.leaderConvertor.toModel(leaderDTO);
        this.leaderService.update(model);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was updated.", Leader.class.getSimpleName(), model.getId());
        }
        return this.leaderConvertor.toResultDTO(model);
    }

    /**
     * 删除操作
     *
     * @param Id 资源ID
     * @return 操作结果
     */

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces=MediaType.APPLICATION_JSON_VALUE)
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.leaderService.delete(id);
        if (log.isInfoEnabled()) {
            log.info("{} instance {} was deleted.", Leader.class.getSimpleName(), id);
        }
        return ResultDTO.success();
    }
}
